package com.javatest.springboot2ratelimit.limitertest;

import com.google.common.util.concurrent.RateLimiter;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2020-07-21 下午1:54
 */
public class App {

    private static final Map<String, RateLimiter> MAP = new ConcurrentHashMap<>();

    public static void main(String[] args) throws InterruptedException {
        RateLimiter rateLimiter = RateLimiter.create(3);
        for (int i = 1; i < 10; i++) {
            double waitTime = rateLimiter.acquire(i);
            System.out.println("cutTime=" + System.currentTimeMillis() + " acq:" + i + " waitTime: " + waitTime);
        }

        for (int i = 1; i < 100; i++) {
            final boolean result = rateLimiter.tryAcquire(1, 0, TimeUnit.SECONDS);
            TimeUnit.MILLISECONDS.sleep(100);
            System.out.println("try acquire token:" + result);
        }
    }
}
